<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="【计算机网络】基于C语言的天气客户端的实现, ZhengN_Blog">
    <meta name="description" content="一、前言上一篇笔记分享了【socket笔记】TCP、UDP通信总结，这一篇分享一个用C语言写的、基于TCP的一个HTTP天气客户端的实现，这个一个控制台应用程序，最终的界面如下：

关于天气预报，之前我已经用STM32+ESP8266wif">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>【计算机网络】基于C语言的天气客户端的实现 | ZhengN_Blog</title>
    <link rel="icon" type="image/jpeg" href="/favicon.jpg">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">ZhengN_Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">ZhengN_Blog</div>
        <div class="logo-desc">
            
            printf(&#34;每天进步一点点~&#34;);
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/zhengnianli/stm32_weather" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/zhengnianli/stm32_weather" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/21.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        【计算机网络】基于C语言的天气客户端的实现
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 20px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/C语言/" target="_blank">
                                <span class="chip bg-color">C语言</span>
                            </a>
                        
                            <a href="/tags/socket/" target="_blank">
                                <span class="chip bg-color">socket</span>
                            </a>
                        
                            <a href="/tags/网络编程/" target="_blank">
                                <span class="chip bg-color">网络编程</span>
                            </a>
                        
                            <a href="/tags/TCP/" target="_blank">
                                <span class="chip bg-color">TCP</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/计算机网络/" class="post-category" target="_blank">
                                计算机网络
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-07-08
                </div>

                
                    
                    <div class="info-break-policy">
                        <i class="fa fa-file-word-o fa-fw"></i>文章字数:&nbsp;&nbsp;
                        3.8k
                    </div>
                    

                    
                    <div class="info-break-policy">
                        <i class="fa fa-clock-o fa-fw"></i>阅读时长:&nbsp;&nbsp;
                        16 分
                    </div>
                    
                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h2><p>上一篇笔记分享了<a href="https://zhengnianli.github.io/2019/06/30/socket-bi-ji-tcp-udp-tong-xin-zong-jie/" target="_blank" rel="noopener">【socket笔记】TCP、UDP通信总结</a>，这一篇分享一个用C语言写的、基于TCP的一个HTTP天气客户端的实现，这个一个控制台应用程序，最终的界面如下：</p>
<p><img src="https://s2.ax1x.com/2019/07/07/ZDKRxA.png" alt></p>
<p>关于天气预报，之前我已经用<code>STM32+ESP8266</code>wifi模块实现过了一遍，感兴趣的可查阅往期笔记：<a href="https://zhengnianli.github.io/2019/06/20/zuo-pin-ji-yu-stm32-de-zhi-neng-tian-qi-yu-bao-xi-tong/" target="_blank" rel="noopener">基于STM32的智能天气预报系统</a>。这次这个基于C语言控制台程序的HTTP客户端的天气解析的代码和之前分享的差不多，只是在那基础上添加修改了一些东西，并配合socket的相关知识实现的，以巩固一下socket编程的知识。下面分享一些实现过程。</p>
<h2 id="二、天气客户端实现的要点"><a href="#二、天气客户端实现的要点" class="headerlink" title="二、天气客户端实现的要点"></a>二、天气客户端实现的要点</h2><p>首先，需要说明的是，这份代码是在windows系统下使用<code>gcc6.3.0</code>进行编译的。</p>
<h3 id="1、秘钥"><a href="#1、秘钥" class="headerlink" title="1、秘钥"></a>1、秘钥</h3><blockquote>
<p>心知天气：<a href="http://www.seniverse.com" target="_blank" rel="noopener">www.seniverse.com</a></p>
</blockquote>
<p>我们完成这个实验必须得到这个上面去注册一个账号才能使用它的天气数据，注册之后每个账户都会有一个<code>私钥</code>，例如：</p>
<blockquote>
<p>私钥 SMEieQjde1C9eXnbE</p>
</blockquote>
<p>这个是我们程序中需要用到。</p>
<h3 id="2、IP和端口"><a href="#2、IP和端口" class="headerlink" title="2、IP和端口"></a>2、IP和端口</h3><p>上一节分享了socket的笔记，我们与服务端通信，需要知道三个重要的信息，分别是：</p>
<ol>
<li>IP地址</li>
<li>端口</li>
<li>传输方式</li>
</ol>
<p>这里的心知天气的IP是<code>116.62.81.138</code>，端口是<code>80</code>，传输方式是<code>TCP</code>，对应的代码如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* 设置要访问的服务器的信息 */</span>
SOCKADDR_IN  ServerSockAddr<span class="token punctuation">;</span>
<span class="token function">memset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ServerSockAddr<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>ServerSockAddr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">// 每个字节都用0填充</span>
ServerSockAddr<span class="token punctuation">.</span>sin_family <span class="token operator">=</span> PF_INET<span class="token punctuation">;</span>                          <span class="token comment" spellcheck="true">// IPv4</span>
ServerSockAddr<span class="token punctuation">.</span>sin_addr<span class="token punctuation">.</span>s_addr <span class="token operator">=</span> <span class="token function">inet_addr</span><span class="token punctuation">(</span>WEATHER_IP_ADDR<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">// 心知天气服务器IP</span>
ServerSockAddr<span class="token punctuation">.</span>sin_port <span class="token operator">=</span> <span class="token function">htons</span><span class="token punctuation">(</span>WEATHER_PORT<span class="token punctuation">)</span><span class="token punctuation">;</span>                 <span class="token comment" spellcheck="true">// 端口</span></code></pre>
<p>这里的<code>WEATHER_IP_ADDR</code>对应的就是<code>116.62.81.138</code>，<code>WEATHER_PORT</code>对应的就是<code>80</code>。</p>
<h3 id="3、GET请求"><a href="#3、GET请求" class="headerlink" title="3、GET请求"></a>3、GET请求</h3><p>HTTP有几种请求方法，我们这里使用的是<code>GET</code>请求。查看心知天气API文档可知，请求地址示例为：</p>
<blockquote>
<p><a href="https://api.seniverse.com/v3/weather/now.json?key=your_api_key&amp;location=beijing&amp;language=zh-Hans&amp;unit=c" target="_blank" rel="noopener">https://api.seniverse.com/v3/weather/now.json?key=your_api_key&amp;location=beijing&amp;language=zh-Hans&amp;unit=c</a></p>
</blockquote>
<p>这是一个天气实况的请求地址示例，其有几个重要的参数：</p>
<p><img src="https://s2.ax1x.com/2019/07/07/ZD8cPf.png" alt></p>
<p>这里的<code>key</code>是个很重要的参数，就是我们前面说的<code>私钥</code>。</p>
<p>我们的天气客户端就是要往天气服务端发送类似这样的<code>GET请求</code>来获取天气数据，具体的请求方法示例为：</p>
<pre class=" language-c"><code class="language-c">GET https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>api<span class="token punctuation">.</span>seniverse<span class="token punctuation">.</span>com<span class="token operator">/</span>v3<span class="token operator">/</span>weather<span class="token operator">/</span>now<span class="token punctuation">.</span>json<span class="token operator">?</span>key<span class="token operator">=</span>2owqvhhd2dd9o9f8<span class="token operator">&amp;</span>location<span class="token operator">=</span>beijing<span class="token operator">&amp;</span>language<span class="token operator">=</span>zh<span class="token operator">-</span>Hans<span class="token operator">&amp;</span>unit<span class="token operator">=</span>c</code></pre>
<p>对应代码如下：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* 秘钥，注意！！如果要用这一份代码，这个一定要改为自己的，因为这个我已经故意改错了，防止有人与我公用一个KEY */</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  KEY    "2owqvhhd2dd9o9f8"        </span><span class="token comment" spellcheck="true">// 这是在心知天气注册后，每个用户自己的一个key</span>

<span class="token comment" spellcheck="true">/* GET请求包 */</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  GET_REQUEST_PACKAGE     \
         "GET https://api.seniverse.com/v3/weather/%s.json?key=%s&amp;location=%s&amp;language=zh-Hans&amp;unit=c\r\n\r\n"</span>

<span class="token comment" spellcheck="true">/* JSON数据包 */</span>    
<span class="token macro property">#<span class="token directive keyword">define</span>  NOW_JSON     "now"</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  DAILY_JSON   "daily"</span>
<span class="token comment" spellcheck="true">//....还用更多其他的天气数据包可查阅心知天气</span>

<span class="token comment" spellcheck="true">/* 组合GET请求包 */</span>
<span class="token function">sprintf</span><span class="token punctuation">(</span>GetRequestBuf<span class="token punctuation">,</span> GET_REQUEST_PACKAGE<span class="token punctuation">,</span> weather_json<span class="token punctuation">,</span> KEY<span class="token punctuation">,</span> location<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/* 发送数据到服务端 */</span>
<span class="token function">send</span><span class="token punctuation">(</span>ClientSock<span class="token punctuation">,</span> GetRequestBuf<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>GetRequestBuf<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>这里简单复习一下<code>sprintf函数</code>的用法：</p>
<p>（1）函数功能：字符串格式化</p>
<p>（2）函数原型：int sprintf(char *string, char *format [,argument,…]);</p>
<p><strong>string</strong>： 这是指向一个字符数组的指针，该数组存储了 C 字符串。</p>
<p><strong>format</strong> ： 这是字符串，包含了要被写入到字符串 str 的文本。</p>
<p><strong>*[argument]..***</strong>.**：根据不同的 format 字符串，函数可能需要一系列的附加参数，每个参数包含了一个要被插入的值，替换了 format 参数中指定的每个 % 标签。</p>
<p>（3）使用示例：</p>
<pre class=" language-c"><code class="language-c"><span class="token function">sprintf</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token string">"%s,%d"</span><span class="token punctuation">,</span> str<span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>假如此时<code>str</code>为<code>&quot;hello&quot;</code>，<code>num</code>为<code>5201314</code>，则此时buf中的内容为：<code>hello，5201314</code>，需要注意的是buf的容量要足够大。</p>
<h3 id="4、天气服务端返回的数据"><a href="#4、天气服务端返回的数据" class="headerlink" title="4、天气服务端返回的数据"></a>4、天气服务端返回的数据</h3><p>天气服务端给我们天气客户端返回的数据为<code>JSON</code>格式数据，可查阅往期笔记<a href="https://zhengnianli.github.io/2019/06/15/json-de-jian-dan-ren-shi/" target="_blank" rel="noopener">JSON的简单认识</a>。我们这个天气客户端只是实现了查询此刻天气（对应的数据包为<code>now.json</code>）及近三天天气情况（对应的数据包为<code>daily.json</code>），如要查询其他信息，可模仿我们这里处理<code>now.json</code>和<code>daily.json</code>的方法，我们用<code>cJson库</code>进行解析。</p>
<p>这个<code>cJson</code>库的下载链接为：</p>
<blockquote>
<p>链接：<a href="https://pan.baidu.com/s/1DQynsdlNyIvsVXmf4W5b8Q" target="_blank" rel="noopener">https://pan.baidu.com/s/1DQynsdlNyIvsVXmf4W5b8Q</a><br>提取码：ww4z</p>
</blockquote>
<p>只要把<code>cJSON.c</code>与<code>cJSON.ｈ</code>放到工程主程序所在目录，然后在主程序中包含头文件<code>JSON.ｈ</code>即可引入该库。如：</p>
<p><img src="https://s2.ax1x.com/2019/07/07/ZDYU2T.png" alt></p>
<p>为了解析<code>now.json</code>和<code>daily.json</code>中的有用数据，我们建立如下结构体：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/* 天气数据结构体 */</span>
<span class="token keyword">typedef</span> <span class="token keyword">struct</span>
<span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">/* 实况天气数据 */</span>
    <span class="token keyword">char</span> id<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">//id</span>
    <span class="token keyword">char</span> name<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">//地名</span>
    <span class="token keyword">char</span> country<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">//国家</span>
    <span class="token keyword">char</span> path<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">//完整地名路径</span>
    <span class="token keyword">char</span> timezone<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">//时区</span>
    <span class="token keyword">char</span> timezone_offset<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">//时差</span>
    <span class="token keyword">char</span> text<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">//天气预报文字</span>
    <span class="token keyword">char</span> code<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">//天气预报代码</span>
    <span class="token keyword">char</span> temperature<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">//气温</span>
    <span class="token keyword">char</span> last_update<span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//最后一次更新的时间</span>


    <span class="token comment" spellcheck="true">/* 今天、明天、后天天气数据 */</span>
    <span class="token keyword">char</span> date<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">//日期</span>
    <span class="token keyword">char</span> text_day<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//白天天气现象文字</span>
    <span class="token keyword">char</span> code_day<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//白天天气现象代码</span>
    <span class="token keyword">char</span> code_night<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">//晚间天气现象代码</span>
    <span class="token keyword">char</span> high<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">//最高温</span>
    <span class="token keyword">char</span> low<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">//最低温</span>
    <span class="token keyword">char</span> wind_direction<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">64</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//风向</span>
    <span class="token keyword">char</span> wind_speed<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">//风速，单位km/h（当unit=c时）</span>
    <span class="token keyword">char</span> wind_scale<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">]</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">//风力等级</span>
<span class="token punctuation">}</span>Weather<span class="token punctuation">;</span></code></pre>
<p>现在看一下<code>now.json</code>和<code>daily.json</code>的内容是怎样的：</p>
<p>（1）<code>now.json</code>示例及解析：</p>
<p><strong>now.json：</strong></p>
<pre class=" language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"results"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
      <span class="token property">"location"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"C23NB62W20TF"</span><span class="token punctuation">,</span>
        <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"西雅图"</span><span class="token punctuation">,</span>
        <span class="token property">"country"</span><span class="token operator">:</span> <span class="token string">"US"</span><span class="token punctuation">,</span>
        <span class="token property">"path"</span><span class="token operator">:</span> <span class="token string">"西雅图,华盛顿州,美国"</span><span class="token punctuation">,</span>
        <span class="token property">"timezone"</span><span class="token operator">:</span> <span class="token string">"America/Los_Angeles"</span><span class="token punctuation">,</span>
        <span class="token property">"timezone_offset"</span><span class="token operator">:</span> <span class="token string">"-07:00"</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token property">"now"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"多云"</span><span class="token punctuation">,</span> //天气现象文字
        <span class="token property">"code"</span><span class="token operator">:</span> <span class="token string">"4"</span><span class="token punctuation">,</span> //天气现象代码
        <span class="token property">"temperature"</span><span class="token operator">:</span> <span class="token string">"14"</span><span class="token punctuation">,</span> //温度，单位为c摄氏度或f华氏度
        <span class="token property">"feels_like"</span><span class="token operator">:</span> <span class="token string">"14"</span><span class="token punctuation">,</span> //体感温度，单位为c摄氏度或f华氏度
        <span class="token property">"pressure"</span><span class="token operator">:</span> <span class="token string">"1018"</span><span class="token punctuation">,</span> //气压，单位为mb百帕或in英寸
        <span class="token property">"humidity"</span><span class="token operator">:</span> <span class="token string">"76"</span><span class="token punctuation">,</span> //相对湿度，<span class="token number">0</span>~<span class="token number">100</span>，单位为百分比
        <span class="token property">"visibility"</span><span class="token operator">:</span> <span class="token string">"16.09"</span><span class="token punctuation">,</span> //能见度，单位为km公里或mi英里
        <span class="token property">"wind_direction"</span><span class="token operator">:</span> <span class="token string">"西北"</span><span class="token punctuation">,</span> //风向文字
        <span class="token property">"wind_direction_degree"</span><span class="token operator">:</span> <span class="token string">"340"</span><span class="token punctuation">,</span> //风向角度，范围<span class="token number">0</span>~<span class="token number">360</span>，<span class="token number">0</span>为正北，<span class="token number">90</span>为正东，<span class="token number">180</span>为正南，<span class="token number">270</span>为正西
        <span class="token property">"wind_speed"</span><span class="token operator">:</span> <span class="token string">"8.05"</span><span class="token punctuation">,</span> //风速，单位为km/h公里每小时或mph英里每小时
        <span class="token property">"wind_scale"</span><span class="token operator">:</span> <span class="token string">"2"</span><span class="token punctuation">,</span> //风力等级，请参考：http<span class="token operator">:</span>//baike.baidu.com/view/<span class="token number">465076</span>.htm
        <span class="token property">"clouds"</span><span class="token operator">:</span> <span class="token string">"90"</span><span class="token punctuation">,</span> //云量，单位%，范围<span class="token number">0</span>~<span class="token number">100</span>，天空被云覆盖的百分比 #目前不支持中国城市#
        <span class="token property">"dew_point"</span><span class="token operator">:</span> <span class="token string">"-12"</span> //露点温度，请参考：http<span class="token operator">:</span>//baike.baidu.com/view/<span class="token number">118348</span>.htm #目前不支持中国城市#
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token property">"last_update"</span><span class="token operator">:</span> <span class="token string">"2015-09-25T22:45:00-07:00"</span> //数据更新时间（该城市的本地时间）
    <span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<p>这里实测了一下，我们普通用户（因为没充钱，哈哈~）申请的<code>now.json</code>数据中，<code>now对象</code>中只有如下三个键值对：</p>
<pre class=" language-json"><code class="language-json"><span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"多云"</span><span class="token punctuation">,</span> //天气现象文字
<span class="token property">"code"</span><span class="token operator">:</span> <span class="token string">"4"</span><span class="token punctuation">,</span> //天气现象代码
<span class="token property">"temperature"</span><span class="token operator">:</span> <span class="token string">"14"</span><span class="token punctuation">,</span> //温度，单位为c摄氏度或f华氏度</code></pre>
<p><strong>now.json的解析函数：</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/*******************************************************************************************************
** 函数: cJSON_NowWeatherParse，解析天气实况数据
**------------------------------------------------------------------------------------------------------
** 参数: JSON：天气数据包   result：数据解析的结果
** 返回: void
********************************************************************************************************/</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">cJSON_NowWeatherParse</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>JSON<span class="token punctuation">,</span> Weather <span class="token operator">*</span>result<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    cJSON <span class="token operator">*</span>json<span class="token punctuation">,</span><span class="token operator">*</span>arrayItem<span class="token punctuation">,</span><span class="token operator">*</span>object<span class="token punctuation">,</span><span class="token operator">*</span>subobject<span class="token punctuation">,</span><span class="token operator">*</span>item<span class="token punctuation">;</span>

    json <span class="token operator">=</span> <span class="token function">cJSON_Parse</span><span class="token punctuation">(</span>JSON<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//解析JSON数据包</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>json <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>          <span class="token comment" spellcheck="true">//检测JSON数据包是否存在语法上的错误，返回NULL表示数据包无效</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Error before: [%s]\n"</span><span class="token punctuation">,</span><span class="token function">cJSON_GetErrorPtr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//打印数据包语法错误的位置</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>arrayItem <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>json<span class="token punctuation">,</span><span class="token string">"results"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//匹配字符串"results",获取数组内容</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> size <span class="token operator">=</span> <span class="token function">cJSON_GetArraySize</span><span class="token punctuation">(</span>arrayItem<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">//获取数组中对象个数</span>
<span class="token macro property">#<span class="token directive keyword">if</span> DEBUG</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"cJSON_GetArraySize: size=%d\n"</span><span class="token punctuation">,</span>size<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token macro property">#<span class="token directive keyword">endif</span></span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>object <span class="token operator">=</span> <span class="token function">cJSON_GetArrayItem</span><span class="token punctuation">(</span>arrayItem<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//获取父对象内容</span>
            <span class="token punctuation">{</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象1：城市地区相关 */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"location"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token comment" spellcheck="true">// 匹配id</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>   
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>id<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配城市名</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>name<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配城市所在的国家</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"country"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>country<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配完整地名路径</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"path"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>  
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>path<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// 保存数据供外部调用    </span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配时区</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"timezone"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>timezone<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">// 保存数据供外部调用    </span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配时差</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"timezone_offset"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>timezone_offset<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象2：今天的天气情况 */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"now"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token comment" spellcheck="true">// 匹配天气现象文字</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"text"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>text<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配天气现象代码</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"code"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>code<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment" spellcheck="true">// 匹配气温</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"temperature"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>temperature<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>    
                <span class="token punctuation">}</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象3：数据更新时间（该城市的本地时间） */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"last_update"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>last_update<span class="token punctuation">,</span> subobject<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>subobject<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">cJSON_Delete</span><span class="token punctuation">(</span>json<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//释放cJSON_Parse()分配出来的内存空间</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>（2）<code>daily.json</code>示例及解析：</p>
<p><strong>daily.json：</strong></p>
<pre class=" language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"results"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    <span class="token property">"location"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"WX4FBXXFKE4F"</span><span class="token punctuation">,</span>
      <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"北京"</span><span class="token punctuation">,</span>
      <span class="token property">"country"</span><span class="token operator">:</span> <span class="token string">"CN"</span><span class="token punctuation">,</span>
      <span class="token property">"path"</span><span class="token operator">:</span> <span class="token string">"北京,北京,中国"</span><span class="token punctuation">,</span>
      <span class="token property">"timezone"</span><span class="token operator">:</span> <span class="token string">"Asia/Shanghai"</span><span class="token punctuation">,</span>
      <span class="token property">"timezone_offset"</span><span class="token operator">:</span> <span class="token string">"+08:00"</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"daily"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>                         //返回指定days天数的结果
      <span class="token property">"date"</span><span class="token operator">:</span> <span class="token string">"2015-09-20"</span><span class="token punctuation">,</span>             //日期
      <span class="token property">"text_day"</span><span class="token operator">:</span> <span class="token string">"多云"</span><span class="token punctuation">,</span>               //白天天气现象文字
      <span class="token property">"code_day"</span><span class="token operator">:</span> <span class="token string">"4"</span><span class="token punctuation">,</span>                  //白天天气现象代码
      <span class="token property">"text_night"</span><span class="token operator">:</span> <span class="token string">"晴"</span><span class="token punctuation">,</span>               //晚间天气现象文字
      <span class="token property">"code_night"</span><span class="token operator">:</span> <span class="token string">"0"</span><span class="token punctuation">,</span>                //晚间天气现象代码
      <span class="token property">"high"</span><span class="token operator">:</span> <span class="token string">"26"</span><span class="token punctuation">,</span>                     //当天最高温度
      <span class="token property">"low"</span><span class="token operator">:</span> <span class="token string">"17"</span><span class="token punctuation">,</span>                      //当天最低温度
      <span class="token property">"precip"</span><span class="token operator">:</span> <span class="token string">"0"</span><span class="token punctuation">,</span>                    //降水概率，范围<span class="token number">0</span>~<span class="token number">100</span>，单位百分比（目前仅支持国外城市）
      <span class="token property">"wind_direction"</span><span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>             //风向文字
      <span class="token property">"wind_direction_degree"</span><span class="token operator">:</span> <span class="token string">"255"</span><span class="token punctuation">,</span>   //风向角度，范围<span class="token number">0</span>~<span class="token number">360</span>
      <span class="token property">"wind_speed"</span><span class="token operator">:</span> <span class="token string">"9.66"</span><span class="token punctuation">,</span>             //风速，单位km/h（当unit=c时）、mph（当unit=f时）
      <span class="token property">"wind_scale"</span><span class="token operator">:</span> <span class="token string">""</span>                  //风力等级
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      <span class="token property">"date"</span><span class="token operator">:</span> <span class="token string">"2015-09-21"</span><span class="token punctuation">,</span>
      <span class="token property">"text_day"</span><span class="token operator">:</span> <span class="token string">"晴"</span><span class="token punctuation">,</span>
      <span class="token property">"code_day"</span><span class="token operator">:</span> <span class="token string">"0"</span><span class="token punctuation">,</span>
      <span class="token property">"text_night"</span><span class="token operator">:</span> <span class="token string">"晴"</span><span class="token punctuation">,</span>
      <span class="token property">"code_night"</span><span class="token operator">:</span> <span class="token string">"0"</span><span class="token punctuation">,</span>
      <span class="token property">"high"</span><span class="token operator">:</span> <span class="token string">"27"</span><span class="token punctuation">,</span>
      <span class="token property">"low"</span><span class="token operator">:</span> <span class="token string">"17"</span><span class="token punctuation">,</span>
      <span class="token property">"precip"</span><span class="token operator">:</span> <span class="token string">"0"</span><span class="token punctuation">,</span>
      <span class="token property">"wind_direction"</span><span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
      <span class="token property">"wind_direction_degree"</span><span class="token operator">:</span> <span class="token string">"157"</span><span class="token punctuation">,</span>
      <span class="token property">"wind_speed"</span><span class="token operator">:</span> <span class="token string">"17.7"</span><span class="token punctuation">,</span>
      <span class="token property">"wind_scale"</span><span class="token operator">:</span> <span class="token string">"3"</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      ...                               //更多返回结果
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token property">"last_update"</span><span class="token operator">:</span> <span class="token string">"2015-09-20T18:00:00+08:00"</span> //数据更新时间（该城市的本地时间）
  <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
<p><strong>daily.json解析函数：</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/*******************************************************************************************************
** 函数: cJSON_DailyWeatherParse，解析近三天天气数据
**------------------------------------------------------------------------------------------------------
** 参数: JSON：天气数据包   result：数据解析的结果
** 返回: void
********************************************************************************************************/</span>
<span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">cJSON_DailyWeatherParse</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>JSON<span class="token punctuation">,</span> Weather <span class="token operator">*</span>result<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    cJSON <span class="token operator">*</span>json<span class="token punctuation">,</span><span class="token operator">*</span>arrayItem<span class="token punctuation">,</span><span class="token operator">*</span>object<span class="token punctuation">,</span><span class="token operator">*</span>subobject<span class="token punctuation">,</span><span class="token operator">*</span>item<span class="token punctuation">,</span><span class="token operator">*</span>sub_child_object<span class="token punctuation">,</span><span class="token operator">*</span>child_Item<span class="token punctuation">;</span>

    json <span class="token operator">=</span> <span class="token function">cJSON_Parse</span><span class="token punctuation">(</span>JSON<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//解析JSON数据包</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>json <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>          <span class="token comment" spellcheck="true">//检测JSON数据包是否存在语法上的错误，返回NULL表示数据包无效</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Error before: [%s]\n"</span><span class="token punctuation">,</span><span class="token function">cJSON_GetErrorPtr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//打印数据包语法错误的位置</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>arrayItem <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>json<span class="token punctuation">,</span><span class="token string">"results"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//匹配字符串"results",获取数组内容</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> size <span class="token operator">=</span> <span class="token function">cJSON_GetArraySize</span><span class="token punctuation">(</span>arrayItem<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">//获取数组中对象个数</span>
<span class="token macro property">#<span class="token directive keyword">if</span> DEBUG</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Get Array Size: size=%d\n"</span><span class="token punctuation">,</span>size<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token macro property">#<span class="token directive keyword">endif</span></span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>object <span class="token operator">=</span> <span class="token function">cJSON_GetArrayItem</span><span class="token punctuation">(</span>arrayItem<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//获取父对象内容</span>
            <span class="token punctuation">{</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象1------结构体location */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"location"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">//匹配子对象1成员"name"</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>name<span class="token punctuation">,</span> item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// 保存数据供外部调用</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象2------数组daily */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"daily"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token keyword">int</span> sub_array_size <span class="token operator">=</span> <span class="token function">cJSON_GetArraySize</span><span class="token punctuation">(</span>subobject<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property">#<span class="token directive keyword">if</span> DEBUG</span>
                    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Get Sub Array Size: sub_array_size=%d\n"</span><span class="token punctuation">,</span>sub_array_size<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property">#<span class="token directive keyword">endif</span></span>
                    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> sub_array_size<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>sub_child_object <span class="token operator">=</span> <span class="token function">cJSON_GetArrayItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token constant">NULL</span><span class="token punctuation">)</span>
                        <span class="token punctuation">{</span>
                            <span class="token comment" spellcheck="true">// 匹配日期</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"date"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>date<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配白天天气现象文字</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"text_day"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>text_day<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配白天天气现象代码</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"code_day"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>code_day<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配夜间天气现象代码</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"code_night"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>code_night<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配最高温度</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"high"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>high<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">//保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配最低温度</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"low"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>low<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配风向</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"wind_direction"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>wind_direction<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配风速，单位km/h（当unit=c时）</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"wind_speed"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>wind_speed<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                            <span class="token comment" spellcheck="true">// 匹配风力等级</span>
                            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>child_Item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>sub_child_object<span class="token punctuation">,</span><span class="token string">"wind_scale"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                            <span class="token punctuation">{</span>
                                <span class="token function">memcpy</span><span class="token punctuation">(</span>result<span class="token operator">-></span>wind_scale<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">,</span><span class="token function">strlen</span><span class="token punctuation">(</span>child_Item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 保存数据</span>
                            <span class="token punctuation">}</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象3------最后一次更新的时间 */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"last_update"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token comment" spellcheck="true">//printf("%s:%s\n",subobject->string,subobject->valuestring);</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">cJSON_Delete</span><span class="token punctuation">(</span>json<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//释放cJSON_Parse()分配出来的内存空间</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="5、获取天气数据并解析"><a href="#5、获取天气数据并解析" class="headerlink" title="5、获取天气数据并解析"></a>5、获取天气数据并解析</h3><p>这个函数就涉及到我们上一节笔记中的socket编程的知识了，先看一下这个函数实现的总体框图：</p>
<p><img src="https://s2.ax1x.com/2019/07/08/ZD2emR.png" alt></p>
<p>下面是函数实现的细节过程：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/*******************************************************************************************************
** 函数: GetWeather，获取天气数据并解析
**------------------------------------------------------------------------------------------------------
** 参数: weather_json：需要解析的json包   location：地名   result：数据解析的结果
** 返回: void
********************************************************************************************************/</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">GetWeather</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>weather_json<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>location<span class="token punctuation">,</span> Weather <span class="token operator">*</span>result<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    SOCKET ClientSock<span class="token punctuation">;</span>
    WSADATA wd<span class="token punctuation">;</span>
    <span class="token keyword">char</span> GetRequestBuf<span class="token punctuation">[</span><span class="token number">256</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> WeatherRecvBuf<span class="token punctuation">[</span><span class="token number">2</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> GbkRecvBuf<span class="token punctuation">[</span><span class="token number">2</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span>  gbk_recv_len <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span>  connect_status <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 初始化操作sock需要的DLL */</span>
    <span class="token function">WSAStartup</span><span class="token punctuation">(</span><span class="token function">MAKEWORD</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>wd<span class="token punctuation">)</span><span class="token punctuation">;</span>  

    <span class="token comment" spellcheck="true">/* 设置要访问的服务器的信息 */</span>
    SOCKADDR_IN  ServerSockAddr<span class="token punctuation">;</span>
    <span class="token function">memset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ServerSockAddr<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>ServerSockAddr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">// 每个字节都用0填充</span>
    ServerSockAddr<span class="token punctuation">.</span>sin_family <span class="token operator">=</span> PF_INET<span class="token punctuation">;</span>                          <span class="token comment" spellcheck="true">// IPv4</span>
    ServerSockAddr<span class="token punctuation">.</span>sin_addr<span class="token punctuation">.</span>s_addr <span class="token operator">=</span> <span class="token function">inet_addr</span><span class="token punctuation">(</span>WEATHER_IP_ADDR<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">// 心知天气服务器IP</span>
    ServerSockAddr<span class="token punctuation">.</span>sin_port <span class="token operator">=</span> <span class="token function">htons</span><span class="token punctuation">(</span>WEATHER_PORT<span class="token punctuation">)</span><span class="token punctuation">;</span>                 <span class="token comment" spellcheck="true">// 端口</span>

    <span class="token comment" spellcheck="true">/* 创建客户端socket */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span> <span class="token operator">==</span> <span class="token punctuation">(</span>ClientSock <span class="token operator">=</span> <span class="token function">socket</span><span class="token punctuation">(</span>PF_INET<span class="token punctuation">,</span> SOCK_STREAM<span class="token punctuation">,</span> IPPROTO_TCP<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"socket error!\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 连接服务端 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span> <span class="token operator">==</span> <span class="token punctuation">(</span>connect_status <span class="token operator">=</span> <span class="token function">connect</span><span class="token punctuation">(</span>ClientSock<span class="token punctuation">,</span> <span class="token punctuation">(</span>SOCKADDR<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>ServerSockAddr<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>SOCKADDR<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"connect error!\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 组合GET请求包 */</span>
    <span class="token function">sprintf</span><span class="token punctuation">(</span>GetRequestBuf<span class="token punctuation">,</span> GET_REQUEST_PACKAGE<span class="token punctuation">,</span> weather_json<span class="token punctuation">,</span> KEY<span class="token punctuation">,</span> location<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 发送数据到服务端 */</span>
    <span class="token function">send</span><span class="token punctuation">(</span>ClientSock<span class="token punctuation">,</span> GetRequestBuf<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>GetRequestBuf<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* 接受服务端的返回数据 */</span>
    <span class="token function">recv</span><span class="token punctuation">(</span>ClientSock<span class="token punctuation">,</span> WeatherRecvBuf<span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">/* utf-8转为gbk */</span>
    <span class="token function">SwitchToGbk</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">unsigned</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>WeatherRecvBuf<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>WeatherRecvBuf<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">unsigned</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>GbkRecvBuf<span class="token punctuation">,</span> <span class="token operator">&amp;</span>gbk_recv_len<span class="token punctuation">)</span><span class="token punctuation">;</span>    
<span class="token macro property">#<span class="token directive keyword">if</span> DEBUG</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"服务端返回的数据为：%s\n"</span><span class="token punctuation">,</span> GbkRecvBuf<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property">#<span class="token directive keyword">endif</span></span>

    <span class="token comment" spellcheck="true">/* 解析天气数据并保存到结构体变量weather_data中 */</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token number">0</span> <span class="token operator">==</span> <span class="token function">strcmp</span><span class="token punctuation">(</span>weather_json<span class="token punctuation">,</span> NOW_JSON<span class="token punctuation">)</span><span class="token punctuation">)</span>        <span class="token comment" spellcheck="true">// 天气实况</span>
    <span class="token punctuation">{</span>
        <span class="token function">cJSON_NowWeatherParse</span><span class="token punctuation">(</span>GbkRecvBuf<span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span>    
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token number">0</span> <span class="token operator">==</span> <span class="token function">strcmp</span><span class="token punctuation">(</span>weather_json<span class="token punctuation">,</span> DAILY_JSON<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 未来三天天气</span>
    <span class="token punctuation">{</span>
        <span class="token function">cJSON_DailyWeatherParse</span><span class="token punctuation">(</span>GbkRecvBuf<span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span>    
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">/* 清空缓冲区 */</span>
    <span class="token function">memset</span><span class="token punctuation">(</span>GetRequestBuf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
    <span class="token function">memset</span><span class="token punctuation">(</span>WeatherRecvBuf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
    <span class="token function">memset</span><span class="token punctuation">(</span>GbkRecvBuf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

    <span class="token comment" spellcheck="true">/* 关闭套接字 */</span>
    <span class="token function">closesocket</span><span class="token punctuation">(</span>ClientSock<span class="token punctuation">)</span><span class="token punctuation">;</span>  

    <span class="token comment" spellcheck="true">/* 终止使用 DLL */</span>
    <span class="token function">WSACleanup</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token punctuation">}</span>
</code></pre>
<h3 id="6、编译"><a href="#6、编译" class="headerlink" title="6、编译"></a>6、编译</h3><p>如何编译这份代码（可在文末进行获取）呢？</p>
<p>这份C代码工程的文件如下：</p>
<p><img src="https://s2.ax1x.com/2019/07/08/ZDRFEt.png" alt></p>
<p>在windows系统下使用<code>gcc</code>编译器编译，编译命令为：</p>
<pre class=" language-c"><code class="language-c">gcc weather_client<span class="token punctuation">.</span>c cJSON<span class="token punctuation">.</span>c utf8togbk<span class="token punctuation">.</span>c <span class="token operator">-</span>o weather_client<span class="token punctuation">.</span>exe <span class="token operator">-</span>lwsock32</code></pre>
<p>如：</p>
<p><img src="https://s2.ax1x.com/2019/07/08/ZDRVC8.png" alt></p>
<p>这里的<code>weather_client.exe</code>就是我们编译生成的可执行文件：<code>天气客户端</code>，双击就可以运行了。此外，<code>-lwsock32</code>参数上一节也有讲过，这个参数用于链接<code>windows</code>下socket编程必须的<code>winsock2</code>这个库。若是使用集成开发环境，则需要把<code>wsock32.lib</code>放在工程目录下，并在我们代码中<code>#include &lt;winsock2.h&gt;</code> 下面加上一行 <code>#pragma comment(lib, &quot;ws2_32.lib&quot;)</code>代码（在IDE里编译本人未验证，有兴趣的朋友可尝试）。</p>
<p>需要说明的是，Windows下默认是没有装gcc的，需要自己进行配置，关于配置及使用<code>mingw</code>（这是个工具包，里面包含有gcc编译器）可查看往期笔记：<a href="https://zhengnianli.github.io/2018/10/05/c-yu-yan-bi-ji-shi-yong-notepad-mingw-lai-kai-fa-c-cheng-xu/" target="_blank" rel="noopener">【C语言笔记】使用notepad++、MinGW来开发C程序</a>、<a href="https://zhengnianli.github.io/2018/12/23/c-yu-yan-bi-ji-windows-ming-ling-xing-xia-bian-yi-c-cheng-xu/" target="_blank" rel="noopener">【C语言笔记】windows命令行下编译C程序</a></p>
<h3 id="7、运行结果示例"><a href="#7、运行结果示例" class="headerlink" title="7、运行结果示例"></a>7、运行结果示例</h3><p><img src="https://s2.ax1x.com/2019/07/08/ZD4Vds.png" alt></p>
<p>此处，只能使用拼音进行搜索，其实也可以做输入汉字进行搜索的功能，只是要进行转码处理，这个功能实现在<a href="https://zhengnianli.github.io/2019/06/20/zuo-pin-ji-yu-stm32-de-zhi-neng-tian-qi-yu-bao-xi-tong/" target="_blank" rel="noopener">基于STM32的智能天气预报系统</a>的代码里已经有做，有兴趣的朋友可以参考这个。</p>
<h2 id="三、代码获取"><a href="#三、代码获取" class="headerlink" title="三、代码获取"></a>三、代码获取</h2><blockquote>
<p><a href="https://github.com/zhengnianli/WeatherClient" target="_blank" rel="noopener">https://github.com/zhengnianli/WeatherClient</a></p>
</blockquote>
<hr>
<p>我的个人博客：<a href="https://www.lizhengnian.cn/">https://www.lizhengnian.cn/</a></p>
<p>我的微信公众号：嵌入式大杂烩</p>
<p>我的CSDN博客：<a href="https://blog.csdn.net/zhengnianli" target="_blank" rel="noopener">https://blog.csdn.net/zhengnianli</a></p>
<p><img src="https://s1.ax1x.com/2020/06/22/NGV7Oe.png" alt></p>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">欢迎关注ZhengN的微信公众号</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <!--<ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微信公众号</a></li>
                    </ul>-->
					<!--
                    <div id="alipay">
                        <img src="" class="reward-img" alt="支付宝打赏二维码">
                    </div>
					-->
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信公众号二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            


        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments input[type=text],
    #vcomments input[type=email],
    #vcomments input[type=url],
    #vcomments textarea {
        box-sizing: border-box;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #42b983;
        font-weight: 500;
        text-decoration: underline;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div id="vcomments" class="card-content"></div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'Vc276uuqkd8y8heuqzShe9p1-gzGzoHsz',
        appKey: 'bUwy6lANpgeVdpcQamMtC8QD',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: '快来快活呀！^_^'
    });
</script>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/07/25/dsp-dsp-xia-zai-cheng-xu-yu-dao-de-wen-ti/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/15.jpg" class="responsive-img" alt="【DSP】下载程序遇到的问题及解决方法">
                        
                        <span class="card-title">【DSP】下载程序遇到的问题及解决方法</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            一、前言最近拿到了一块TI官方的DSP开发板，芯片型号是：TMS320F280049C，板载调试器XDS110。但是下载程序时遇到了一点问题，现记录总结如下：


TMS320F280049C特性：


二、下载程序时遇到的问题及解决方法之
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2019-07-25
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/嵌入式/" class="post-category" target="_blank">
                                    嵌入式
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/嵌入式/" target="_blank">
                        <span class="chip bg-color">嵌入式</span>
                    </a>
                    
                    <a href="/tags/DSP/" target="_blank">
                        <span class="chip bg-color">DSP</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/06/30/ji-suan-ji-wang-luo-tcp-udp-tong-xin-zong-jie/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="【计算机网络】TCP、UDP通信总结">
                        
                        <span class="card-title">【计算机网络】TCP、UDP通信总结</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            一、什么是socket？Socket的英文原义是“孔”或“插座”。在编程中，Socket被称做套接字，是网络通信中的一种约定。Socket编程的应用无处不在，我们平时用的QQ、微信、浏览器等程序，都与Socket编程有关。我们平时使用浏览器
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-06-30
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/计算机网络/" class="post-category" target="_blank">
                                    计算机网络
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/socket/" target="_blank">
                        <span class="chip bg-color">socket</span>
                    </a>
                    
                    <a href="/tags/网络编程/" target="_blank">
                        <span class="chip bg-color">网络编程</span>
                    </a>
                    
                    <a href="/tags/TCP/" target="_blank">
                        <span class="chip bg-color">TCP</span>
                    </a>
                    
                    <a href="/tags/UDP/" target="_blank">
                        <span class="chip bg-color">UDP</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由&copy;<a href="https://zhengnianli.github.io/" target="_blank">ZhengNian</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a> 的
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">hexo-theme-matery</a>主题搭建.

            
                &nbsp;<i class="fa fa-area-chart"></i>&nbsp;站点总字数:&nbsp;
                <span class="white-color">187.2k</span>
            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/zhengnianli" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fa fa-github"></i>
    </a>



    <a href="mailto:2238184039@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fa fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2238184039" class="tooltipped" data-tooltip="QQ联系我: 2238184039" data-position="top" data-delay="50">
        <i class="fa fa-qq"></i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fa fa-rss"></i>
    </a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->



    <script src="/libs/others/clicklove.js"></script>


    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>